﻿using BLL;
using Microsoft.AspNetCore.Mvc;
using NetHelperCore;
using System.Diagnostics;

namespace ServiceManage.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProgramController : ControllerBase
    {

        /// <summary>
        /// 获取需要管理的程序列表
        /// </summary>
        /// <returns></returns>
        public ServiceResultModel ProgramListGet()
        {
            ServiceResultModel _rm = new ServiceResultModel()
            {
                Success = -1,
                Result = null,
                ErrorMessage = ""
            };

            try
            {
                _rm.Success = 1;

                _rm.Result = ConfigBLL.ProgramInfoList;
            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(NetHelperCore.CommonHelper.GetNewID(1));
                LogHelper.WriteLog(LogHelper.LogType.Error, _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                _rm.Success = 0;
                _rm.ErrorMessage = "错误代码:" + _errorID;
            }


            return _rm;
        }

        /// <summary>
        /// 添加需要管理的程序
        /// </summary>
        /// <returns></returns>
        public ServiceResultModel ProgramAdd()
        {
            ServiceResultModel _result = new ServiceResultModel()
            {
                Success = -1,
                Result = null,
                ErrorMessage = ""
            };

            try
            {
                string _param = Request.Form["ParamStr"].ToString();
                if (!string.IsNullOrWhiteSpace(_param))
                {
                    JsonHelper _json = new JsonHelper(_param);
                    string _filePath = _json.GetVar("FilePath");
                    int _port = Convert.ToInt32(_json.GetVar("Port"));
                    if (string.IsNullOrWhiteSpace(_filePath))
                    {
                        _result.Success = 0;
                        _result.ErrorMessage = "程序路径不可为空";
                        return _result;
                    }
                    if (ConfigBLL.ProgramInfoList == null)
                    {
                        ConfigBLL.ProgramInfoList = new List<ProgramInfo>();
                    }
                    ConfigBLL.ProgramInfoList.Add(new ProgramInfo()
                    {
                        ID = NetHelperCore.CommonHelper.GetNewID(1),
                        FilePath = _filePath,
                        Port = _port
                    });
                    ConfigBLL.SaveConfig();
                }

                _result.Success = 1;
                _result.Result = ConfigBLL.ProgramInfoList;
            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(NetHelperCore.CommonHelper.GetNewID(1));
                LogHelper.WriteLog(LogHelper.LogType.Error, _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                _result.Success = 0;
                _result.ErrorMessage = "错误代码:" + _errorID;

            }


            return _result;
        }

        /// <summary>
        /// 更新需要管理的程序
        /// </summary>
        /// <returns></returns>
        public ServiceResultModel ProgramUpdate()
        {
            ServiceResultModel _rm = new ServiceResultModel()
            {
                Success = -1,
                Result = null,
                ErrorMessage = ""
            };

            try
            {
                string _param = Request.Form["ParamStr"].ToString();
                if (!string.IsNullOrWhiteSpace(_param))
                {
                    JsonHelper _json = new JsonHelper(_param);
                    long _id = Convert.ToInt64(_json.GetVar("ID"));
                    string _filePath = _json.GetVar("FilePath");
                    int _port = Convert.ToInt32(_json.GetVar("Port"));

                    if (_id == 0)
                    {
                        _rm.Success = -2;
                        _rm.ErrorMessage = "未找到配置的程序信息";
                        return _rm;
                    }
                    if (string.IsNullOrWhiteSpace(_filePath))
                    {
                        _rm.Success = 0;
                        _rm.ErrorMessage = "程序路径不可为空";
                        return _rm;
                    }
                    ProgramInfo _serviceInfo = ConfigBLL.ProgramInfoList.Find(it => it.ID == _id);
                    if (_serviceInfo == null)
                    {
                        _rm.Success = -2;
                        _rm.ErrorMessage = "未找到配置的程序信息";
                        return _rm;
                    }
                    else
                    {
                        _serviceInfo.FilePath = _filePath;
                        _serviceInfo.Port = _port;
                        ConfigBLL.SaveConfig();
                        _rm.Success = 1;
                    }
                }
            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(NetHelperCore.CommonHelper.GetNewID(1));
                LogHelper.WriteLog(LogHelper.LogType.Error, _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                _rm.Success = 0;
                _rm.ErrorMessage = "错误代码:" + _errorID;
            }


            return _rm;
        }

        /// <summary>
        /// 删除需要管理的程序
        /// </summary>
        /// <returns></returns>
        public ServiceResultModel ProgramDelete()
        {
            ServiceResultModel _rm = new ServiceResultModel()
            {
                Success = -1,
                Result = null,
                ErrorMessage = ""
            };

            try
            {
                string _param = Request.Form["ParamStr"].ToString();
                if (!string.IsNullOrWhiteSpace(_param))
                {
                    JsonHelper _json = new JsonHelper(_param);
                    long _id = Convert.ToInt64(_json.GetVar("ID"));
                    string _filePath = _json.GetVar("FilePath");
                    int _port = Convert.ToInt32(_json.GetVar("Port"));

                    if (_id == 0)
                    {
                        _rm.Success = -2;
                        _rm.ErrorMessage = "未找到配置的程序信息";
                        return _rm;
                    }
                    ProgramInfo _serviceInfo = ConfigBLL.ProgramInfoList.Find(it => it.ID == _id);
                    if (_serviceInfo == null)
                    {
                        _rm.Success = -2;
                        _rm.ErrorMessage = "未找到配置的程序信息";
                        return _rm;
                    }
                    else
                    {
                        ConfigBLL.ProgramInfoList.Remove(_serviceInfo);
                        ConfigBLL.SaveConfig();
                        _rm.Success = 1;
                    }

                }
            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(NetHelperCore.CommonHelper.GetNewID(1));
                LogHelper.WriteLog(LogHelper.LogType.Error, _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "", new StackTrace(new StackFrame(true)).GetFrame(0).GetMethod().Name);
                _rm.Success = 0;
                _rm.ErrorMessage = "错误代码:" + _errorID;
            }


            return _rm;
        }



    }
}
